package com.example.androidwebview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * js 中 调用native的代码
 */
public class JsCallNative extends Activity {
    private Context mCtx;
    TextView tvTxt1;
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.js_call_native);

        //
        mCtx = this;
        tvTxt1 = (TextView) findViewById(R.id.tv_txt1);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.loadUrl("file:///android_asset/jscallnative/index.html");
        // 增加js接口 object = 某个类的实例，该类的一个方法有JavascriptInterface annotation
        webView.addJavascriptInterface(this,"android");
    }


    @JavascriptInterface
    public void showToast(String content) {
        Toast.makeText(mCtx, content, Toast.LENGTH_SHORT).show();
    }
}
